#include <stdio.h>
#include <math.h>

int main(void) {
    int k;
    scanf("%d", &k);
    for(int i = 0; i < k; i++) {
        int m, r = 1;
        scanf("%d", &m);
        int n;
        for (n = 0; n < m; n++) {
            r %= m;
            if (r == 0) {
                m = -1;
                break;
            }
            r = r * 10 + 1;
        }
        if (m == -1) {
            printf("%d\n", n + 1);
        } else {
            printf("0\n");
        }
    }
    return 0;
}
